# -*- coding: utf-8 -*-
#


from service import Service
from sippeer.utils.logutil import logger

#logging.config.fileConfig("../config/logging.conf")
#logger = logging.getLogger("ServiceManager")

class ServiceManager:
    """
    It stores all the services the client can provide.
    It can discovery the service module dynamically.
    Implemented using singleton design pattern.
    """
    
    class __ServiceManager:
        """
        """
        
        def __init__(self):
            
            
            self._serviceList = list()
        
        def serviceDiscovery(self):
            pass
        
        def addService(self, service_name):
            
            try:
                if service_name == None or service_name == None:
                    logger.error("ServiceManager::addService(): Service name or Service instance is NULL!")
                    return
                self._serviceList.append(service_name)
                logger.debug("ServiceManager::addService(): add service name: " + service_name)
                
            except KeyError:
                logger.error("ServiceManager::addService(): failed for KeyError")
                return 
             
        
        def getService(self, service_name):
            """
            Return the service's class name
            service class name = service name + "Service"
            """
            try:
                if service_name == None or service_name == "":
                    logger.error("ServiceManager::findService(): Service name is NULL!")
                    return None
                
                if self._serviceList.count(service_name) > 0:
                    logger.debug("ServiceManager::findService(): Find service by service name " + service_name)
                    return service_name + "Service"
                else:
                    logger.debug("ServiceManager::findService(): Can not find service by service name " + service_name)
                    return None
                
            except KeyError:
                logger.error("ServiceManager::findService(): service_name got KeyError exception!")
                return None
            
        def getServiceImportPath(self, service_name):
            """
            Return the service's import path. 
            """
            
        def findService(self, service_name):
            try:
                if service_name == None or service_name == "":
                    logger.error("ServiceManager::findService(): Service name is NULL!")
                    return False
                
                if self._serviceList.count(service_name) > 0:
                    logger.debug("ServiceManager::findService(): Find service by service name " + service_name)
                    return True
                else:
                    logger.debug("ServiceManager::findService(): Can not find service by service name " + service_name)
                    return False
                
            except KeyError:
                logger.error("ServiceManager::findService(): service_name got KeyError exception!")
                return False
            
        def removeService(self, service_name):
            
            try:
                if service_name == None or service_name == "":
                    logger.error("ServiceManager::removeService(): service name is NULL!")
                    return False
                
                if self._serviceList.count(service_name) > 0:
                    del self._serviceList[self._serviceList.index(service_name)]
                    logger.debug("ServiceManager::removeService(): service " + service_name + " has been removed")
                    return True
                
            except KeyError:
                logger.error("ServiceManager::removeService(): service name got KeyError!")
                return False
                
        
        def hasService(self):
            
            if self._serviceList.__len__ == 0:
                return False
            return True
        
        def getServiceNameList(self):
            pass
        
    __instance = None
    
    def getInstance():
        """
        @summary:  Singleton Class
        """
        if ServiceManager.__instance == None:
            ServiceManager.__instance = ServiceManager.__ServiceManager()          
        return ServiceManager.__instance
    getInstance = staticmethod(getInstance)
    
    
    
     